﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Transactions;
using DataAccess;
using Common;

#region VƯƠNG TRÍ LỢI
namespace BusinessLogic
{
    public class PhieuThu : MarshalByRefObject  // rat hay
    {
        static void LuuViet(string hanhDong, string maNhanVien, string noiDung, string tenDangNhap, string thoiGianThucHien)
        {
            try
            {
                Entities.LuuVietHeThong lv = new Entities.LuuVietHeThong();
                lv.HanhDong = hanhDong;
                lv.MaNhanVien = maNhanVien;
                lv.NoiDung = noiDung;
                lv.TenDangNhap = tenDangNhap;
                lv.ThoiGianThucHien = thoiGianThucHien;
                new LuuVietHeThong().LuuViet(lv);
            }
            catch
            {
                return;
            }
        }

        private static SqlCommand cmd;
        private static SqlDataReader dr;
        private static SqlConnection cn;
     
        /// <summary>
        /// select
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static Entities.PhieuThu[] Select()
        {
            try
            {
                string sql = Common.Constants.PhieuThu.SelectPhieuThu;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.PhieuThu row = new Entities.PhieuThu();

                    row.PhieuThuID = int.Parse(dr[Common.Constants.PhieuThu.PhieuThuID].ToString());
                    row.MaPhieuThu = dr[Common.Constants.PhieuThu.MaPhieuThu].ToString();
                    row.NgayLap = DateTime.Parse(dr[Common.Constants.PhieuThu.NgayLap].ToString());
                    row.MaDoiTuong = dr[Common.Constants.PhieuThu.MaDoiTuong].ToString();
                    row.KhoanMuc = dr[Common.Constants.PhieuThu.KhoanMuc].ToString();
                    row.DoiTuong = dr[Common.Constants.PhieuThu.DoiTuong].ToString();
                    row.TenDoiTuong = dr[Common.Constants.PhieuThu.TenDoiTuong].ToString();
                    row.DiaChi = dr[Common.Constants.PhieuThu.DiaChi].ToString();
                    row.MaSoThue = dr[Common.Constants.PhieuThu.MaSoThue].ToString();
                    row.NguoiNopTien = dr[Common.Constants.PhieuThu.NguoiNopTien].ToString();
                    row.MaTKNganHang = dr[Common.Constants.PhieuThu.MaTKNganHang].ToString();
                    row.CoTaiKhoan = dr[Common.Constants.PhieuThu.CoTaiKhoan].ToString();
                    row.NoTaiKhoan = dr[Common.Constants.PhieuThu.NoTaiKhoan].ToString();
                    row.TongTienThanhToan = dr[Common.Constants.PhieuThu.TongTienThanhToan].ToString();
                    row.MaKho = dr[Common.Constants.PhieuThu.MaKho].ToString();
                    row.GhiChu = dr[Common.Constants.PhieuThu.GhiChu].ToString();
                    row.Deleted = bool.Parse(dr[Common.Constants.PhieuThu.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return null;

                Entities.PhieuThu[] arrC = new Entities.PhieuThu[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.PhieuThu)arr[i];
                }

                //Giai phong bo nho
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return arrC;
            }
            catch { return new Entities.PhieuThu[0]; }
        }

       /// <summary>
       /// select theo dia diem
       /// </summary>
       /// <param name="hdbh1"></param>
       /// <returns></returns>
        public static Entities.PhieuThu[] SelectTheoDiaDiem(string MaKho)
       {
           try
           {
               string sql = Common.Constants.PhieuThu.SelectPhieuThuTheoDiaDiem;

               cn = DataAccess.Connection.OpenConnection();
               cmd = new SqlCommand(sql, cn);
               cmd.Parameters.Add(Common.Constants.PhieuChi.MaKho, SqlDbType.VarChar, 50).Value = MaKho;
               dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

               ArrayList arr = new ArrayList();
               while (dr.Read())
               {
                   Entities.PhieuThu row = new Entities.PhieuThu();

                   row.PhieuThuID = int.Parse(dr[Common.Constants.PhieuThu.PhieuThuID].ToString());
                   row.MaPhieuThu = dr[Common.Constants.PhieuThu.MaPhieuThu].ToString();
                   row.NgayLap = DateTime.Parse(dr[Common.Constants.PhieuThu.NgayLap].ToString());
                   row.MaDoiTuong = dr[Common.Constants.PhieuThu.MaDoiTuong].ToString();
                   row.KhoanMuc = dr[Common.Constants.PhieuThu.KhoanMuc].ToString();
                   row.DoiTuong = dr[Common.Constants.PhieuThu.DoiTuong].ToString();
                   row.TenDoiTuong = dr[Common.Constants.PhieuThu.TenDoiTuong].ToString();
                   row.DiaChi = dr[Common.Constants.PhieuThu.DiaChi].ToString();
                   row.MaSoThue = dr[Common.Constants.PhieuThu.MaSoThue].ToString();
                   row.NguoiNopTien = dr[Common.Constants.PhieuThu.NguoiNopTien].ToString();
                   row.MaTKNganHang = dr[Common.Constants.PhieuThu.MaTKNganHang].ToString();
                   row.CoTaiKhoan = dr[Common.Constants.PhieuThu.CoTaiKhoan].ToString();
                   row.NoTaiKhoan = dr[Common.Constants.PhieuThu.NoTaiKhoan].ToString();
                   row.TongTienThanhToan = dr[Common.Constants.PhieuThu.TongTienThanhToan].ToString();
                   row.MaKho = dr[Common.Constants.PhieuThu.MaKho].ToString();
                   row.GhiChu = dr[Common.Constants.PhieuThu.GhiChu].ToString();
                   row.Deleted = bool.Parse(dr[Common.Constants.PhieuThu.Deleted].ToString());
                   arr.Add(row);
               }
               int n = arr.Count;
               if (n == 0) return null;

               Entities.PhieuThu[] arrC = new Entities.PhieuThu[n];
               for (int i = 0; i < n; i++)
               {
                   arrC[i] = (Entities.PhieuThu)arr[i];
               }

               //Giai phong bo nho
               arr = null;
               cmd.Connection.Dispose();
               cn.Close();
               cn = null;
               
               return arrC;
           }
           catch { return new Entities.PhieuThu[0]; }
       }
      
        /// <summary>
        /// insert
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static bool InsertUpdate(Entities.PhieuThu phieuthu)
        {
            TransactionScope ts = null;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.PhieuThu.InsertUpdatePhieuThu;
                    cn = DataAccess.Connection.OpenConnection();
                    cmd = new SqlCommand(sql, cn);

                    cmd.Parameters.Add(Common.Constants.PhieuThu.HanhDong, SqlDbType.NVarChar, 20).Value = phieuthu.HanhDong;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.PhieuThuID, SqlDbType.NVarChar, 50).Value = phieuthu.PhieuThuID;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.MaPhieuThu, SqlDbType.NVarChar, 50).Value = phieuthu.MaPhieuThu;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.NgayLap, SqlDbType.DateTime).Value = phieuthu.NgayLap;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.MaDoiTuong, SqlDbType.VarChar, 50).Value = phieuthu.MaDoiTuong;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.KhoanMuc, SqlDbType.NVarChar, 200).Value = phieuthu.KhoanMuc;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.DoiTuong, SqlDbType.NVarChar, 20).Value = phieuthu.DoiTuong;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.TenDoiTuong, SqlDbType.NVarChar, 50).Value = phieuthu.TenDoiTuong;

                    cmd.Parameters.Add(Common.Constants.PhieuThu.DiaChi, SqlDbType.NVarChar, 50).Value = phieuthu.DiaChi;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.MaSoThue, SqlDbType.VarChar, 50).Value = phieuthu.MaSoThue;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.NguoiNopTien, SqlDbType.NVarChar, 200).Value = phieuthu.NguoiNopTien;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.MaTKNganHang, SqlDbType.NVarChar, 50).Value = phieuthu.MaTKNganHang;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.CoTaiKhoan, SqlDbType.VarChar, 50).Value = phieuthu.CoTaiKhoan;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.NoTaiKhoan, SqlDbType.VarChar, 50).Value = phieuthu.NoTaiKhoan;

                    cmd.Parameters.Add(Common.Constants.PhieuThu.TongTienThanhToan, SqlDbType.Float).Value = phieuthu.TongTienThanhToan;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.MaKho, SqlDbType.VarChar, 50).Value = phieuthu.MaKho;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.MaNhanVien, SqlDbType.VarChar, 50).Value = phieuthu.MaNhanVien;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.GhiChu, SqlDbType.NVarChar, 200).Value = phieuthu.GhiChu;
                    cmd.Parameters.Add(Common.Constants.PhieuThu.Deleted, SqlDbType.Bit).Value = phieuthu.Deleted;
                    int i = cmd.ExecuteNonQuery();
                    bool kt = false;
                    if (i == 1)
                    {
                        kt = true;


                        if (phieuthu.HanhDong == Common.Constants.Insert)
                        {
                            LuuViet
                                (
                                "Thêm",
                                phieuthu.TaiKhoan.MaNhanVien,
                                "Thêm phiếu thu " + phieuthu.MaPhieuThu,
                                phieuthu.TaiKhoan.TenDangNhap,
                                DateTime.Now.ToShortDateString()
                                );
                        }
                        else
                        {
                            LuuViet
                                (
                                "Sửa",
                                phieuthu.TaiKhoan.MaNhanVien,
                                "Sửa phiếu thu " + phieuthu.MaPhieuThu,
                                phieuthu.TaiKhoan.TenDangNhap,
                                DateTime.Now.ToShortDateString()
                                );
                        }


                    }
                    else
                        kt = false;
                    cmd.Connection.Dispose();
                    cn.Close();
                    cn = null;

                    ts.Complete();
                    return kt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static bool Delete(Entities.PhieuThu ptdel)
        {
            try
            {
                bool kt = false;
                string sql = Common.Constants.PhieuThu.DeletePhieuThu;


                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
          
                cmd.Parameters.Add(Common.Constants.PhieuThu.HanhDong, SqlDbType.NVarChar, 20).Value = ptdel.HanhDong;
                cmd.Parameters.Add(Common.Constants.PhieuThu.PhieuThuID, SqlDbType.Int).Value = ptdel.PhieuThuID;
                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                {
                    kt = true;

                    if (ptdel.HanhDong == Common.Constants.Insert)
                    {
                        LuuViet
                            (
                            "Xóa",
                            ptdel.TaiKhoan.MaNhanVien,
                            "Xóa phiếu thu " + ptdel.MaPhieuThu,
                            ptdel.TaiKhoan.TenDangNhap,
                            DateTime.Now.ToShortDateString()
                            );
                    }

                }
                else
                    kt = false;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return kt;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// select
        /// </summary>
        /// <param name="hdbh1"></param>
        /// <returns></returns>
        public static Entities.TKKeToan[] SelectCoNo(string str)
        {
            try
            {
                string sql = Common.Constants.LayCombox.sp_LayTKKT;

                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.LayCombox.HanhDong, SqlDbType.VarChar, 50).Value = str;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.TKKeToan row = new Entities.TKKeToan();

                    row.MaTKKeToan = dr[Common.Constants.TKKeToan.MaTKKeToan].ToString();
                    row.TenTaiKhoan = dr[Common.Constants.TKKeToan.TenTaiKhoan].ToString();
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) return null;

                Entities.TKKeToan[] arrC = new Entities.TKKeToan[n];
                for (int i = 0; i < n; i++)
                {
                    arrC[i] = (Entities.TKKeToan)arr[i];
                }

                //Giai phong bo nho
                arr = null;
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                
                return arrC;
            }
            catch { return new Entities.TKKeToan[0]; }
        }
    }
}

#endregion